import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Headers } from '@nestjs/common';
import { TemplateService } from './template.service';
import { CreateTemplateDto } from './dto/create-template.dto';
import { UpdateTemplateDto } from './dto/update-template.dto';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { QueryDto } from './dto/query.dto';
import { UserInfoDto } from '../model/dto/user-info.dto';

@Controller('template')
export class TemplateController {
  constructor(private readonly templateService: TemplateService) { }

  @Post()
  @ApiOperation({
    summary: 'Create a template',
    description: 'Create a template',
  })
  @ApiResponse({
    status: 201,
    description: 'The template has been successfully created.',
    type: CreateTemplateDto
  })
  create(@Body() createTemplateDto: CreateTemplateDto) {
    return this.templateService.create(createTemplateDto);
  }

  @Post('track')
  @ApiOperation({
    summary: 'Create a template with user tracking',
    description: 'Create a template while tracking which user created it',
  })
  @ApiResponse({
    status: 201,
    description: 'The template has been successfully created with user tracking.',
    type: CreateTemplateDto
  })
  createAndTrack(
    @Body() createTemplateDto: CreateTemplateDto,
    @Headers('user') user: UserInfoDto
  ) {
    return this.templateService.createAndTrack(createTemplateDto, user && JSON.parse(user as unknown as string)
    );
  }

  @Get()
  @ApiOperation({
    summary: 'Get all templates',
    description: 'Get all templates',
  })
  @ApiResponse({
    status: 200,
    description: 'The templates has been successfully retrieved.',
    type: [UpdateTemplateDto]
  })
  findAll(@Query() query: QueryDto) {
    return this.templateService.findAll(query);
  }

  @Get(':id')
  @ApiOperation({
    summary: 'Get a template',
    description: 'Get a template',
  })
  @ApiResponse({
    status: 200,
    description: 'The template has been successfully retrieved.',
    type: [UpdateTemplateDto]
  })
  findOne(@Param('id') id: string) {
    return this.templateService.findOne(+id);
  }

  @Patch(':id')
  @ApiOperation({
    summary: 'Update a template',
    description: 'Update a template',
  })
  @ApiResponse({
    status: 200,
    description: 'The template has been successfully updated.',
    type: UpdateTemplateDto
  })
  update(@Param('id') id: string, @Body() updateTemplateDto: UpdateTemplateDto) {
    return this.templateService.update(+id, updateTemplateDto);
  }

  @Delete(':id')
  @ApiOperation({
    summary: 'Delete a template',
    description: 'Delete a template',
  })
  @ApiResponse({
    status: 200,
    description: 'The template has been successfully deleted.',
    type: UpdateTemplateDto
  })
  remove(@Param('id') id: string) {
    return this.templateService.remove(+id);
  }
}
